1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import java.io.*;
34 import java.net.*;
35
36 public class B4722333 implements HttpCallback {
37
38 static int count = 0;
39
40 static String [][] expected = {
41
42 {"basic", "foo"},
43 {"basic", "foobar"},
44 {"digest", "biz"},
45 {"digest", "bizbar"},
46 {"digest", "foobiz"}
47 };
48
49 public void request (HttpTransaction req) {
50 try {
51 if (count % 2 == 1 ) {
52 req.setResponseEntityBody ("Hello .");
53 req.sendResponse (200, "Ok");
54 req.orderlyClose();
55 } else {
56 switch (count) {
57 case 0:
58 req.addResponseHeader ("Connection", "close");
59 req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\"");
60 req.addResponseHeader ("WWW-Authenticate", "Foo realm=\"bar\"");
61 req.sendResponse (401, "Unauthorized");
62 req.orderlyClose();
63 break;
64 case 2:
65 req.addResponseHeader ("Connection", "close");
66 req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foobar\" Foo realm=\"bar\"");
67 req.sendResponse (401, "Unauthorized");
68 break;
69 case 4:
70 req.addResponseHeader ("Connection", "close");
71 req.addResponseHeader ("WWW-Authenticate", "Digest realm=biz domain=/foo nonce=thisisanonce ");
72 req.addResponseHeader ("WWW-Authenticate", "Basic realm=bizbar");
73 req.sendResponse (401, "Unauthorized");
74 req.orderlyClose();
75 break;
76 case 6:
77 req.addResponseHeader ("Connection", "close");
78 req.addResponseHeader ("WWW-Authenticate", "Digest realm=\"bizbar\" domain=/biz nonce=\"hereisanonce\" Basic realm=\"foobar\" Foo realm=\"bar\"");
79 req.sendResponse (401, "Unauthorized");
80 req.orderlyClose();
81 break;
82 case 8:
83 req.addResponseHeader ("Connection", "close");
84 req.addResponseHeader ("WWW-Authenticate", "Foo p1=1 p2=2 p3=3 p4=4 p5=5 p6=6 p7=7 p8=8 p9=10 Digest realm=foobiz domain=/foobiz nonce=newnonce");
85 req.addResponseHeader ("WWW-Authenticate", "Basic realm=bizbar");
86 req.sendResponse (401, "Unauthorized");
87 req.orderlyClose();
88 break;
89 }
90 }
91 count ++;
92 } catch (IOException e) {
93 e.printStackTrace();
94 }
95 }
96
97 static void read (InputStream is) throws IOException {
98 int c;
99 System.out.println ("reading");
100 while ((c=is.read()) != -1) {
101 System.out.write (c);
102 }
103 System.out.println ("");
104 System.out.println ("finished reading");
105 }
106
107
108 static void client (String u) throws Exception {
109 URL url = new URL (u);
110 System.out.println ("client opening connection to: " + u);
111 URLConnection urlc = url.openConnection ();
112 InputStream is = urlc.getInputStream ();
113 read (is);
114 is.close();
115 }
116
117 static HttpServer server;
118
119 public static void main (String[] args) throws Exception {
120 MyAuthenticator auth = new MyAuthenticator ();
121 Authenticator.setDefault (auth);
122 try {
123 server = new HttpServer (new B4722333(), 1, 10, 0);
124 System.out.println ("Server started: listening on port: " + server.getLocalPort());
125 client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html");
126 client ("http://localhost:"+server.getLocalPort()+"/ASD/d3/x.html");
127 client ("http://localhost:"+server.getLocalPort()+"/biz/d3/x.html");
128 client ("http://localhost:"+server.getLocalPort()+"/bar/d3/x.html");
129 client ("http://localhost:"+server.getLocalPort()+"/fuzz/d3/x.html");
130 } catch (Exception e) {
131 if (server != null) {
132 server.terminate();
133 }
134 throw e;
135 }
136 int f = auth.getCount();
137 if (f != expected.length) {
138 except ("Authenticator was called "+f+" times. Should be " + expected.length);
139 }
140 server.terminate();
141 }
142
143 public static void except (String s) {
144 server.terminate();
145 throw new RuntimeException (s);
146 }
147
148 static class MyAuthenticator extends Authenticator {
149 MyAuthenticator () {
150 super ();
151 }
152
153 int count = 0;
154
155 public PasswordAuthentication getPasswordAuthentication ()
156 {
157 System.out.println ("Auth called");
158 String scheme = getRequestingScheme();
159 System.out.println ("getRequestingScheme() returns " + scheme);
160 String prompt = getRequestingPrompt();
161 System.out.println ("getRequestingPrompt() returns " + prompt);
162
163 if (!scheme.equals (expected [count][0])) {
164 B4722333.except ("wrong scheme received, " + scheme + " expected " + expected [count][0]);
165 }
166 if (!prompt.equals (expected [count][1])) {
167 B4722333.except ("wrong realm received, " + prompt + " expected " + expected [count][1]);
168 }
169 count ++;
170 return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
171 }
172
173 public int getCount () {
174 return (count);
175 }
176 }
177
178 }